//
//  BPShareViewController.m
//  BeautyPicture
//
//  Created by 沈振邦 on 15/8/31.
//  Copyright (c) 2015年 szb. All rights reserved.
//

#import "BPProductShareViewController.h"
#import "BPshareSdkView.h"
#import <ShareSDK/ShareSDK.h>
#import "MBProgressHUD+MJ.h"
#import "NavView.h"
#import "orderDetailVC.h"
#import "AccountTool.h"
#import "BPhttpTool.h"
#import "composeVC.h"
#import "AppDelegate.h"

#define shareUrl @"http://ttmy.len.so/home/appinvite/workshow/id/"

@interface BPProductShareViewController ()<BPShareDelegate,navDelegate>

@end

@implementation BPProductShareViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupNav];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setUpButtonAndTextLabel];
}

- (void)setupNav
{
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"首页" style:UIBarButtonItemStyleDone target:self action:@selector(saveDIYImage)];
    NavView *leftNavView = [[NavView alloc] initWithFrame:CGRectMake(0, 0, 70, 40)];
    leftNavView.navLabel.text = self.leftNav;
    leftNavView.delegate = self;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftNavView];
    self.title = @"下单";
}

- (void)saveDIYImage
{
    AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
    if (delegate.isEdit == YES) {
        [self.navigationController popToRootViewControllerAnimated:YES];
    }else{
        delegate.backHome = YES;
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

- (void)navTap:(UITapGestureRecognizer *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma - mark 设置按钮和文字
- (void)setUpButtonAndTextLabel
{
    CGFloat imageX = BPScreenW * (1.0 / 2.0) - 40;
    CGFloat imageY = BPScreenNavY + 20.0f;
    UIImageView *rightImage = [[UIImageView alloc] init];
    rightImage.image = [UIImage imageNamed:@"sureImage"];
    rightImage.frame = CGRectMake(imageX, imageY, 30, 30);
    [self.view addSubview:rightImage];
    
    CGFloat textX = CGRectGetMaxX(rightImage.frame) + 10;
    CGFloat textW = 80;
    UILabel *saveFinishText = [[UILabel alloc] initWithFrame:CGRectMake( textX, imageY, textW, rightImage.height)];
    saveFinishText.text = @"搞定!";
    saveFinishText.font = [UIFont systemFontOfSize:18.0f];
    [self.view addSubview:saveFinishText];
    
    CGFloat promptY = CGRectGetMaxY(rightImage.frame) + 15.0;
    UILabel *promptText = [[UILabel alloc] initWithFrame:CGRectMake(0, promptY, BPScreenW, 15.0f)];
    promptText.textAlignment = NSTextAlignmentCenter;
    promptText.textColor = [UIColor blackColor];
    promptText.font = [UIFont systemFontOfSize:18.0f];
    promptText.text = @"大作已保存至“我的作品”";
    [self.view addSubview:promptText];
    
    UIButton *PrintButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [PrintButton setTitle:@"继续编辑" forState:UIControlStateNormal];
    [self addBtn:PrintButton];
    PrintButton.backgroundColor = pinkBtnColor;
    PrintButton.center = CGPointMake((BPScreenW / 3.0) - 15.0f, promptText.height + promptY + 40.0f);
    [PrintButton addTarget:self action:@selector(reEditing) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *goOnEditing = [UIButton buttonWithType:UIButtonTypeCustom];
    [goOnEditing setTitle:@"前往印刷" forState:UIControlStateNormal];
    [self addBtn:goOnEditing];
    goOnEditing.center = CGPointMake((BPScreenW / 3.0) * 2.0 + 15.0f, PrintButton.centerY);
    [goOnEditing addTarget:self action:@selector(BPQuickPrint) forControlEvents:UIControlEventTouchUpInside];
    
    CGFloat shareY = CGRectGetMaxY(PrintButton.frame) + 20.0f;
    BPshareSdkView *shareView = [[BPshareSdkView alloc] initWithFrame:CGRectMake(0, shareY, BPScreenW, BPScreenH - shareY)];
    shareView.delegate = self;
    [self.view addSubview:shareView];
}

- (void)addBtn:(UIButton *)button
{
    button.size = CGSizeMake(106.0f, 32.0f);
    button.backgroundColor = BPGlobaThemeColor;
    [button.layer setCornerRadius:17.0f];
    [self.view addSubview:button];
}

#pragma - mark 按钮点击事件
- (void)BPQuickPrint
{
    orderDetailVC *orderVc = [[orderDetailVC alloc] init];
    orderVc.webName = @"productDetail.html";
    orderVc.title = @"作品详情";
    orderVc.imagePaths = self.ImagePath;
    orderVc.productModel = self.productData;
    orderVc.assetArry = self.assetArry;
    [self.navigationController pushViewController:orderVc animated:YES];
}

- (void)reEditing
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)shareToolView:(BPshareSdkView *)shareView didClickButton:(BPShareButtonType)buttonType
{
    switch (buttonType) {
        case BPShareToWeibo:
            [self shareToApp:[shareView viewWithTag:buttonType] shareType:ShareTypeSinaWeibo];
            break;
        case BPShareToWX:
            [self shareToApp:[shareView viewWithTag:buttonType] shareType:ShareTypeWeixiSession];
            break;
        case BPShareToQQ:
            [self shareToApp:[shareView viewWithTag:buttonType] shareType:ShareTypeQQ];
            break;
        case BPShareToWXFriend:
            [self shareToApp:[shareView viewWithTag:buttonType] shareType:ShareTypeWeixiTimeline];
            break;
        case BPshareToQQKJ:
            [self shareToApp:[shareView viewWithTag:buttonType] shareType:ShareTypeQQSpace];
        case BPShareToBP:
            [self shareToBP];
            break;
        default:
            break;
    }
}

- (void)shareToApp:(id)sender shareType:(ShareType)type
{
    
    id<ISSContent> publishContent = [ShareSDK content:self.productData.title
                                       defaultContent:@""
                                                image:[ShareSDK imageWithPath:self.ImagePath[0]]
                                                title:@"天天美印"
                                                  url:[NSString stringWithFormat:@"%@%@",shareUrl,self.productData.workID]
                                          description:@"天天美印个人作品"
                                            mediaType:SSPublishContentMediaTypeNews];
    
    
    //创建弹出菜单容器
    id<ISSContainer> container = [ShareSDK container];
    [container setIPadContainerWithView:sender arrowDirect:UIPopoverArrowDirectionUp];
    
    id<ISSAuthOptions> authOptions = [ShareSDK authOptionsWithAutoAuth:YES
                                                         allowCallback:YES
                                                         authViewStyle:SSAuthViewStyleFullScreenPopup
                                                          viewDelegate:nil
                                               authManagerViewDelegate:nil];
    
    id<ISSShareOptions> shareOptions = [ShareSDK defaultShareOptionsWithTitle:nil
                                                              oneKeyShareList:[NSArray defaultOneKeyShareList]
                                                               qqButtonHidden:NO
                                                        wxSessionButtonHidden:NO
                                                       wxTimelineButtonHidden:NO
                                                         showKeyboardOnAppear:NO
                                                            shareViewDelegate:nil
                                                          friendsViewDelegate:nil                                                     picViewerViewDelegate:nil];
    
    
    //分享类型
    [ShareSDK showShareViewWithType:type
                          container:container
                            content:publishContent
                      statusBarTips:YES
                        authOptions:authOptions
                       shareOptions:shareOptions
                             result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
                                 
                                 if (state == SSPublishContentStateSuccess)
                                 {
                                     [MBProgressHUD showSuccess:@"发表成功"];
                                 }
                                 else if (state == SSPublishContentStateFail)
                                 {
                                     [MBProgressHUD showError:@"发表失败"];
                                 }
                             }];
}


- (void)shareToBP
{
    composeVC *compose = [[composeVC alloc] init];
    compose.workID = self.productData.workID;
    compose.imagePaths = self.ImagePath;
    compose.block = ^(){
    };
    [self.navigationController pushViewController:compose animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
